<?php
/**
 * 后台筛选管理
 *
 * 后台筛选管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\SearchService\SearchBackService;
use App\Services\ProductTempService\ProductTempTool;
use Illuminate\Support\Facades\Response;

class SearchController extends Controller
{
    public function __construct(SearchBackService $search_back_service,ProductTempTool $productTempTool)
    {
        $this->search_back_service = $search_back_service;
        $this->productTempTool = $productTempTool;
    }

    /**
     * 获取筛选列表
     * @param int $product_temp_category_id
     * @return mixed
     */
    public function getList($product_temp_category_id = 0)
    {
        $product_temp_categorys = $this->productTempTool->getAllProductTempCategorys(1);
        $searchs = $this->search_back_service->getList($product_temp_category_id,8);
        return view('backstage.search.list')
            ->with('searchs',$searchs)
            ->with('product_temp_categorys',$product_temp_categorys)
            ->with('product_temp_category_id',$product_temp_category_id);
    }

    /**
     * 添加筛选页面
     * @return mixed
     */
    public function getAdd(){
        $this->search_back_service->setParameter([
            'table' => 'searchs',
            'where' => [['product_temp_categoty_id','=',0],['pid','=',0],['state','=',1],['type','<>',1]],
            'select'=> ['search_id','product_temp_categoty_id','search_name','resource_id','level','pid','type','is_radio','sort'],
            'order' => 'sort',
            'sort'  => 'desc'
        ]);
        $searchs = $this->search_back_service->getAll();
        $product_temp_categorys = $this->productTempTool->getAllProductTempCategorys(1);
        return view('backstage.search.add')
            ->with('product_temp_categorys',$product_temp_categorys)
            ->with('searchs',$searchs);
    }

    /**
     * 搜索模板分类
     * @param Request $request
     * @return mixed
     */
    public function postSearch(Request $request){
        $product_temp_category_id = $request->input('product_temp_category_id');
        $this->search_back_service->setParameter([
            'table' => 'searchs',
            'where' => [['product_temp_categoty_id','=',$product_temp_category_id],['pid','=',0],['state','=',1],['type','<>',1]],
            'select'=> ['search_id','product_temp_categoty_id','search_name','resource_id','level','pid','type','is_radio','sort'],
            'order' => 'sort',
            'sort'  => 'desc'
        ]);
        $searchs = $this->search_back_service->getAll();
        return Response::json($searchs);
    }

    /**
     * 搜索样式
     * @return mixed
     */
    public function postSearchStyle(){
        $attrs = $this->search_back_service->get_all_style_list();
        return Response::json(array('state'=>1,'result'=>$attrs,'message'=>'获取成功'));
    }

    /**
     * 添加筛选
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'search_name' => 'required|max:20',
            'product_temp_categoty_id' => 'required|integer',
            'pid' => 'required|integer',
            'type' => 'required|integer',
            'level' => 'integer',
            'resource_id' => 'string',
            'is_radio'=> 'boolean'
        ]);
        $search_name = trim($request->input('search_name'));
        $product_temp_categoty_id = $request->input('product_temp_categoty_id');
        $pid = $request->input('pid');
        $type = $request->input('type');
        $level = $request->input('level');
        $resource_id = $request->input('resource_id');
        $is_radio = $request->input('is_radio');
        $result = $this->search_back_service->save_search(array('search_name'=>$search_name,'product_temp_categoty_id'=>$product_temp_categoty_id,
            'pid'=>$pid,'type'=>$type,'level'=>$level,'resource_id'=>$resource_id,'is_radio'=>$is_radio));
        if(empty($result)){
            return redirect()->back()->with('error_message','保存失败');
        }
        if(isset($result['state']) && $result['state'] != 'M00000'){
            return redirect()->back()->with('error_message',$result['message']);
        }

        $error_save = array();
        if($pid == 0 && $type != 1){
            //添加子分类
            $child_search_name = $request->input('child_search_name');
            $child_resource_id = $request->input('child_resource_id');
            if(count($child_search_name) != count($child_resource_id)){
                return redirect()->back()->with('success_message','一级搜索保存成功，二级搜索内容不全，未能保存');
            }
            foreach($child_search_name as $key => $value){
                if($value == ''){
                    $error_save[] = '二级搜索'.($key+1).'未填写名称保存失败';
                    continue;
                }
                //保存
                $resource_id = $child_resource_id[$key];
                $result_child = $this->search_back_service->save_search(array('search_name'=>$value,'product_temp_categoty_id'=>$product_temp_categoty_id,
                    'pid'=>$result,'type'=>$type,'level'=>$level,'resource_id'=>$resource_id,'is_radio'=>$is_radio));
                if(empty($result_child)){
                    $error_save[] = '二级搜索'.($key+1).'保存失败';
                    continue;
                }
                if(isset($result_child['state']) && $result_child['state'] != 'M00000'){
                    $error_save[] = '二级搜索'.($key+1).$result_child['message'];
                    continue;
                }
            }
        }
        if(empty($error_save)){
            return redirect()->back()->with('success_message','保存成功');
        }else{
            $message = '';
            foreach($error_save as $item){
                $message .= $item;
            }
            return redirect()->back()->with('success_message','一级搜索保存成功，'.$message);
        }
    }

    /**
     * 新增子搜索
     */
    public function getAddChild($search_id){
        $search = $this->search_back_service->get_search_by_search_id(intval($search_id));
        if(empty($search)){
            return redirect()->back()->with('error_message','未找到该搜索');
        }
        return view('backstage.search.add_child_search')->with('search',$search);
    }

    /**
     * 删除筛选
     * @param $search_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($search_id){
        $result = $this->search_back_service->delete_search(intval($search_id));
        if($result['state'] != 'M00000'){
            return redirect()->back()->with('error_message',$result['message']);
        }else{
            return redirect()->back()->with('success_message',$result['message']);
        }
    }

    /**
     * 修改排序
     * @param Request $request
     * @return mixed
     */
    public function postUpdateSort(Request $request)
    {
        $search_id = intval($request->input('search_id'));
        $sort = intval($request->input('sort'));
        $result = $this->search_back_service->update_search_sort($search_id,$sort);
        return empty($result) ? Response::json(array('message'=>'修改失败','result'=>'','state'=>'M00001'))
            : Response::json(array('message'=>'修改成功','result'=>'','state'=>'M00000'));
    }
}
